<template>
	<view>
		<view class="ticket">
			<view class="ticket-header">
				<view class="refund">
					<text>{{cinemaName}}</text>
					<text style="position: relative;z-index: 9999;" v-if="statusText!=='已退款' && isRefound"
						@click="refund">退票</text>
				</view>
				<view class="ticket-film">
					<view class="ticket-film-name">
						<text>{{filmData.name}}</text>
						<view class="session-date" style="color: #999;margin-top: 10rpx;">
							<text>{{getDate(startTime)}}</text>
							<text style="margin-left: 10rpx;">{{getTime(startTime)}}:00</text>
						</view>
						<view style="margin-top: 10rpx;">
							<text class="screen-name">{{screenName}}</text>
						</view>

						<view style="margin-top: 10rpx;">
							<view class="screen-seat" v-for="(item,index) in seatsList" :key="item.seatCode">
								<text class="seat-price">{{item.rowNum}}排{{item.columnNum}}座<text
										style="color: #E53F31;margin-left: 10rpx;"
										v-if="priceList[index] > 0">{{priceList[index]}}元</text>
								</text><text style="color: #E53F31;font-size: 22rpx;"
									v-if="serviceFee[index] > 0">(含{{serviceFee[index]}}元服务费)</text>
								</text>
							</view>
						</view>
					</view>
					<image :src="filmData.imgUrl" mode=""></image>
				</view>
				<view class="session">

					<view class="session-screen">


					</view>
				</view>
			</view>
			<view class="film-code">
				<view class="">
					<text>序列号：{{resolveCode(ticketCode.PrintNo)}}</text>
					<text>验证码：{{resolveCode(ticketCode.VerifyCode)}}</text>
				</view>
			</view>
			<view class="film-qr">
				<!-- 	<view v-if="!showCode" @click="showCodeFn">
					<text class="title">展开取票码</text>
					
				</view> -->
				<view class="show-qr" v-if="showCode">
					<view class="qr-title">
						<text>取电影票</text>
					</view>
					<view>
						<!-- <canvas style="width: 220px; height: 220px;background:pink;" canvas-id="myQrcode"></canvas> -->
						<uqr-code :text="qrCode" :show='true' :size="180"></uqr-code>
					</view>
				</view>
			</view>

			<u-popup mode='center' :round='10' :show="showPassword" @click-overlay="showPassword=false">
				<view class="input-password">
					<u--form labelPosition="left" :model="memberRefundParams" ref="uForm" labelWidth='auto'
						labelAlign='right'>
						<u-form-item label="密码：" borderBottom ref="item4">
							<u--input name="密码" password required placeholder="密码"
								v-model="memberRefundParams.password" />
						</u-form-item>
					</u--form>
					<u-button shape="circle" type="primary" @click="cardRefund"
						custom-style="margin-top:30rpx;width:60vw;height:60rpx;">确定</u-button>
				</view>
			</u-popup>
		</view>
		<view class="cinema-info">
			<view class="">
				<view class="" style="font-size: 28rpx;">
					{{cinemaName}}
				</view>
				<view class="" style="font-size: 22rpx;color: #999;">
					{{cinemaAddress}}
				</view>
			</view>
			<view class="" @click="callCinema" style="position: relative;z-index: 99999;">
				<u-icon name="phone" color='#9bbd83' size='60rpx' />
			</view>
		</view>
	</view>
</template>

<script>
	import {
		queryOrderInfo,
		wxRefundTicket,
		memberRefundTicket,
		quertTicketList
	} from '../../common/http/api.js'
	import UqrCode from '../uqrcode/uqrcode.vue'
	export default {
		props: ["orderCode", "statusText"],
		components: {
			UqrCode
		},
		data() {
			return {
				showCode: true,
				memberRefundParams: {
					orderNo: "",
					cardCode: "",
					password: ""
				},
				ticketCode: {
					VerifyCode: '',
					PrintNo: ''
				},
				filmData: {},
				screenName: "",
				cinemaName: '',
				cinemaAddress: '',
				cinemaPhone: '',
				startTime: '',
				seatsList: [],
				qrCode: '',
				showPassword: false,
				isRefound: false,
				quertTicketList: {
					openId: uni.getStorageSync("openId"),
					pageNo: 1,
				},
				priceList: [],
				serviceFee: []
			};
		},
		watch: {
			orderCode(newCode) {
				queryOrderInfo(newCode).then(res => {
					const {
						code,
						data
					} = res
					if (code === "SUCCESS") {
						this.ticketCode = {
							VerifyCode: data.verifyCode,
							PrintNo: data.printNo,
						}
						this.checkTime(data.startTime)
						this.filmData = data.films[0]
						this.memberRefundParams.cardCode = data.cardCode;
						this.memberRefundParams.orderNo = data.orderCode
						let printNo = data.printNo.slice(10, 19);
						let verifyCode = data.verifyCode.slice(10, 19);
						this.qrCode = printNo + '|' + verifyCode;
						this.seatsList = data.seats
						this.startTime = data.startTime
						this.screenName = data.screenName
					}
				})
			}
		},

		created() {
			this.cinemaName = this.$store.state.cinemaName
			this.cinemaAddress = this.$store.state.cinemaAddress
			this.cinemaPhone = this.$store.state.cinemaPhone
			this.queryInfo()
		},
		computed: {
			resolveCode() {
				return code => {
					return code.slice(10, 19)
				}
			},
			getDate() {
				return date => {

					return date.slice(0, 10)
				}
			},
			getTime() {
				return date => {
					return date.slice(11, 16)
				}
			},
			getName() {
				return code => {
					const {
						screens
					} = this.$store.state
					console.log(screens);
					console.log(code);
					const index = screens.find(item => item.ScreenCode === code)
					return index ? index.ScreenName : ''
				}
			},
		},
		methods: {
			queryInfo() {
				quertTicketList(this.quertTicketList).then(res => {
					const {
						data

					} = res
					// this.orderList = data
					const curOrder = data.find(item => item.orderCode == this.orderCode)

					this.priceList = curOrder.seatList.map(item => item.payPrice)
					this.serviceFee = curOrder.seatList.map(item => item.serviceFee)

				})
			},
			callCinema() {

				uni.makePhoneCall({
					phoneNumber: this.cinemaPhone
				})
			},
			refund() {
				uni.showModal({
					content: "是否退票",
					success: () => {
						if (this.memberRefundParams.cardCode) {
							this.showPassword = true
						} else {
							wxRefundTicket(this.ticketCode).then(res => {
								const {
									code
								} = res
								if (code === "SUCCESS") {
									uni.redirectTo({
										url: "../../pages/orderList/orderList"
									})
								} else {
									uni.showToast({
										title: "数据异常",
										icon: "none"
									})
								}
							})
						}
					}
				})
			},
			checkTime(time) {
				const current = new Date().getTime()
				const filmTime = new Date(time).getTime()
				const refoundSwitch = this.$store.state.cinemaConfig.switchRefund
				if (filmTime > current && refoundSwitch == 1) this.isRefound = true
			},
			cardRefund() {
				memberRefundTicket(this.memberRefundParams).then(res => {

					const {
						msg,
						code
					} = res
					if (code === 'SUCCESS') {
						uni.showToast({
							title: msg,
							icon: "none"
						})
						setTimeout(() => {
							uni.redirectTo({
								url: "../../pages/orderList/orderList"
							})
						}, 1500)
					} else {
						uni.showToast({
							title: msg,
							icon: "none"
						})
					}
				})
			},
			change(e) {
				this.memberRefundParams.password = e.detail

			},
			showCodeFn() {
				// const _this = this
				this.showCode = true
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cinema-info {
		width: 90vw;
		margin: 20rpx auto 0 auto;
		padding: 30rpx 20rpx 30rpx 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.input-password {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 80vw;
		height: 200rpx;
		padding: 30rpx;
	}

	.ticket {
		width: 90vw;
		margin: -180rpx auto 0 auto;
		padding: 30rpx 20rpx 0 20rpx;
		background-color: #fff;
		border-radius: 20rpx;

		.ticket-header::after {
			content: '';
			width: 40rpx;
			height: 40rpx;
			background-color: #f0f0f5;
			position: absolute;
			border-radius: 50%;
			bottom: -20rpx;
			left: -50rpx;
		}

		.ticket-header::before {
			content: '';
			width: 40rpx;
			height: 40rpx;
			background-color: #f0f0f5;
			position: absolute;
			border-radius: 50%;
			bottom: -20rpx;
			right: -50rpx;
		}

		.ticket-header {
			border-bottom: 1px dashed #999;
			position: relative;

			.refund {
				width: 100%;
				display: flex;
				justify-content: space-between;
				padding-bottom: 5rpx;
				border-bottom: 1px solid #f0f0f5;

				text {
					// line-height: 60rpx;
					font-size: 26rpx;
					color: royalblue;
				}
			}

			.ticket-film {
				display: flex;
				justify-content: space-between;
				margin-top: 10rpx;

				image {
					width: 155rpx;
					height: 190rpx;
				}

				.ticket-film-name {
					display: flex;
					flex-direction: column;

					// justify-content: space-between;

				}
			}

			.session {
				display: flex;
				margin-top: 20rpx;

				.session-date {
					display: flex;
					flex-direction: column;

					text {

						font-size: 28rpx;
					}

					text:first-child {
						font-size: 24rpx;
						color: #999;
					}
				}

				.session-screen {
					display: flex;
					flex-direction: column;

					margin-left: 150rpx;
					padding-bottom: 30rpx;

					.screen-name {
						font-size: 24rpx;
						color: #999;

					}

					.screen-seat {
						width: 380rpx;
						font-size: 28rpx;
						display: flex;
						flex-wrap: wrap;
						justify-content: flex-end;

						.seat-price {
							margin-left: 20rpx;
						}
					}
				}
			}
		}

		.film-code {
			position: relative;
			height: 220rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-bottom: 1px dashed #999;

			view {
				display: flex;
				flex-direction: column;
				font-size: 24rpx;
				color: #999;
				border: 1px solid #999;
				border-radius: 15rpx;
				padding: 10rpx 80rpx;

				text {
					line-height: 60rpx;
				}
			}
		}

		.film-code::after {
			content: '';
			width: 40rpx;
			height: 40rpx;
			background-color: #f0f0f5;
			position: absolute;
			border-radius: 50%;
			bottom: -20rpx;
			left: -50rpx;
		}

		.film-code::before {
			content: '';
			width: 40rpx;
			height: 40rpx;
			background-color: #f0f0f5;
			position: absolute;
			border-radius: 50%;
			bottom: -20rpx;
			right: -50rpx;
		}

		.film-qr {

			padding-bottom: 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 28rpx;
			color: #999;

			.title {
				line-height: 60rpx;
			}

			.show-qr {
				font-size: 30rpx;
				color: black;
				display: flex;
				// justify-content: center;
				flex-direction: column;
				align-items: center;

				.qr-title {
					margin: 30rpx 0 50rpx 0;
					width: 88vw;

				}

				image {
					width: 250rpx;
					height: 250rpx;
				}
			}
		}
	}
</style>